package com.woniu.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.common.entity.Result;
import com.woniu.common.utils.IdWorker;
import com.woniu.common.utils.StringUtils;
import com.woniu.mall.VO.HotProductVO;
import com.woniu.mall.VO.ProductDetailVO;
import com.woniu.mall.VO.ProductListVO;
import com.woniu.mall.config.MallOrderQueueConfig;
import com.woniu.mall.constant.MallPayInfoConstants;
import com.woniu.mall.constant.OrderConstants;
import com.woniu.mall.entity.CheckOrderInfo;
import com.woniu.mall.feign.OrderFeignClient;
import com.woniu.mall.mapper.MallProductMapper;
import com.woniu.mall.mapper.MallStoreMapper;
import com.woniu.mall.pojo.MallOrder;
import com.woniu.mall.pojo.MallProduct;
import com.woniu.mall.pojo.MallStore;
import com.woniu.mall.service.IMallProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.pay.entity.PayInfo;
import com.woniu.pay.feign.PayFeignClient;
import com.woniu.user.vo.UserLoginVO;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 坤
 * @since 2023-05-22
 */
@Service
public class MallProductServiceImpl extends ServiceImpl<MallProductMapper, MallProduct> implements IMallProductService {
    @Resource
    private MallProductMapper productMapper;
    @Resource
    private MallStoreMapper storeMapper;
    @Autowired
    private PayFeignClient payFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<ProductListVO> listSearch(String title) {
        List<ProductListVO> productList = productMapper.listSearch(title);
        return productList;
    }

    @Override
    public Integer reduceStock(Integer pid) {
        Integer result = productMapper.reduceStock(pid);
        return result;
    }

    @Override
    public Integer addStock(Integer pid) {
        Integer result = productMapper.addStock(pid);
        return result;
    }

    @Override
    public Integer addProduct(MallProduct product) {
        return productMapper.insert(product);
    }

    @Override
    public Integer deleteProduct(Integer pid) {
        return productMapper.deleteById(pid);
    }

    @SneakyThrows
    @Override
    public Object recharge(UserLoginVO userLoginVO, BigDecimal amount) {
        //创建订单号
        IdWorker idWorker = new IdWorker(1,0);
        Integer orderId = (int) idWorker.nextId();
        String outTradeNo = orderId+"";
        //创建订单
        MallOrder order = new MallOrder();
        order.setOrderId(orderId);
        order.setOrderStatus(OrderConstants.ORDER_STATUS_UNPAY);
        order.setTotalPrice(amount);
        order.setUserId(userLoginVO.getId());
        orderFeignClient.addOrder(order);

//        创建支付对象
        PayInfo payInfo = new PayInfo();
        payInfo.setOutTradeNo(outTradeNo);
        payInfo.setTotalAmount(amount);
        payInfo.setSubject(OrderConstants.RECHARGE_BIT_COIN);
        payInfo.setReturnUrl(MallPayInfoConstants.RETURN_URL);
        payInfo.setNotifyUrl(MallPayInfoConstants.NOTIFY_URL);
//        发起支付
        Object form = payFeignClient.pay(payInfo).getData();

//        死信队列解决超时问题 TODO
        CheckOrderInfo checkOrderInfo = new CheckOrderInfo();
        checkOrderInfo.setOrderNo(outTradeNo);
        checkOrderInfo.setProductName(payInfo.getSubject());
        rabbitTemplate.convertAndSend(MallOrderQueueConfig.ORDER_EXCHANGE, MallOrderQueueConfig.X_DEAD_LETTER_ROUTING_KEY, checkOrderInfo);
//        将订单编号放到redis中，方便后续查询订单
        redisTemplate.boundListOps(OrderConstants.QUERY_ORDER_TRADE_NO).leftPush(outTradeNo);

//        跳转到支付页面
        return form;
    }


    @Override
    public String getCategoryIcon(String category) {
        String categoryIcon = productMapper.getCategoryIcon(category);
        return categoryIcon;
    }

    @Override
    public List<ProductListVO> getByCategory(String category) {
        List<ProductListVO> productList = productMapper.listByCategory(category);
        return productList;
    }

    @Override
    public List<HotProductVO> hotProduct(Integer num) {
        List<HotProductVO> hotProducts = productMapper.hotProduct(num);
        return hotProducts;
    }


    @Override
    public MallStore getStore(Integer pid) {
        Integer storeId = productMapper.getStoreId(pid);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("store_id", storeId);
        MallStore store = storeMapper.selectOne(wrapper);
        return store;
    }

    @Override
    public ProductDetailVO showDetail(Integer pid) {
        ProductDetailVO productDetailVO = productMapper.showDetail(pid);
        return productDetailVO;
    }

    @Override
    public MallProduct getProduct(Integer productId){
        MallProduct product = productMapper.getProduct(productId);
        return product;
    }

    @Override
    public List<MallProduct> getAllProduct() {
        List<MallProduct> productList = productMapper.getAllProduct();
        return productList;
    }

    @Override
    public void updateStock(Integer productId, Integer itemQuantity) {
        productMapper.updateStock(productId,itemQuantity);
    }
}
